package tt.dz.biz.dao.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tt.dz.common.BusinessException;
import tt.dz.common.DzConstants;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.vo.CityVO;
import tt.dz.vo.CountryVO;
import tt.dz.vo.DistrictVO;
import tt.dz.vo.ProvinceVO;
import tt.dz.vo.object.Node;

@Component("commonDAOHelpHandler")
public class CommonDAOHelpHandler
{
  public static final String BIZ_DISTRICT_NODE_KEY = "DISTRICT_NODE";
  public static final String BIZ_COUNTRY_NODE_KEY = "COUNTRY_NODE";
  public static final String REDIS_DISTRICT_NODE_KEY = DzConstants.REDIS_PRE_PUBLIC_KEY + "DISTRICT_NODE";
  @Resource(name="commonDAO")
  private CommonDAO commonDAO;
  
  public List<Node> convertNodeByProvince(List<ProvinceVO> values, Map<String, List<Node>> subProviceNodeMap)
  {
    List<Node> cList = new ArrayList();
    Node tmpNode = null;
    List<Node> dList = null;
    boolean isEmptyDMap = (null == subProviceNodeMap) || (subProviceNodeMap.isEmpty());
    boolean isEmptySub = false;
    for (ProvinceVO provinceVO : values)
    {
      tmpNode = new Node();
      tmpNode.setCode(provinceVO.getProvinceCode());
      tmpNode.setName(provinceVO.getProvinceName());
      tmpNode.setLevel("1");
      if (!isEmptyDMap)
      {
        dList = (List)subProviceNodeMap.get(provinceVO.getProvinceCode());
        isEmptySub = (null == dList) || (dList.isEmpty());
        tmpNode.setIsLeaf(isEmptySub ? "1" : "0");
        if (!isEmptySub) {
          tmpNode.setSubNodes(dList);
        }
      }
      cList.add(tmpNode);
    }
    return cList;
  }
  
  public List<Node> convertNodeByCity(List<CityVO> values, Map<String, List<Node>> subCityNodeMap)
  {
    List<Node> cList = new ArrayList();
    Node tmpNode = null;
    List<Node> dList = null;
    boolean isEmptyDMap = (null == subCityNodeMap) || (subCityNodeMap.isEmpty());
    boolean isEmptySub = false;
    for (CityVO cityVO : values)
    {
      tmpNode = new Node();
      tmpNode.setCode(cityVO.getCityCode());
      tmpNode.setName(cityVO.getCityName());
      tmpNode.setLevel("2");
      if (!isEmptyDMap)
      {
        dList = (List)subCityNodeMap.get(cityVO.getCityCode());
        isEmptySub = (null == dList) || (dList.isEmpty());
        tmpNode.setIsLeaf(isEmptySub ? "1" : "0");
        if (!isEmptySub) {
          tmpNode.setSubNodes(dList);
        }
      }
      cList.add(tmpNode);
    }
    return cList;
  }
  
  public List<Node> convertNodeByDistrict(List<DistrictVO> values)
  {
    List<Node> cList = new ArrayList();
    Node tmpNode = null;
    for (DistrictVO districtVO : values)
    {
      tmpNode = new Node();
      tmpNode.setCode(districtVO.getDistrictCode());
      tmpNode.setName(districtVO.getDistrictName());
      tmpNode.setLevel("3");
      tmpNode.setIsLeaf("1");
      tmpNode.setSubNodes(null);
      cList.add(tmpNode);
    }
    return cList;
  }
  
  public Map<String, CountryVO> getCountryMap()
  {
    List<CountryVO> countryList = this.commonDAO.listAllCountry();
    Map<String, CountryVO> countryMap = new HashMap();
    countryMap.put("COUNTRY_NODE", countryList.get(0));
    return countryMap;
  }
  
  public Map<String, List<ProvinceVO>> getProvinceMap()
  {
    List<ProvinceVO> provinceList = this.commonDAO.listAllProvince();
    Map<String, List<ProvinceVO>> map = new HashMap();
    for (ProvinceVO provinceVO : provinceList)
    {
      String countryCode = provinceVO.getCountryCode();
      if (map.containsKey(countryCode))
      {
        ((List)map.get(countryCode)).add(provinceVO);
      }
      else
      {
        List<ProvinceVO> subList = new ArrayList();
        subList.add(provinceVO);
        map.put(countryCode, subList);
      }
    }
    return map;
  }
  
  public Map<String, List<CityVO>> getCityMap()
  {
    List<CityVO> cityList = this.commonDAO.listAllCity();
    Map<String, List<CityVO>> map = new HashMap();
    for (CityVO cityVO : cityList)
    {
      String provinceCode = cityVO.getProvinceCode();
      if (map.containsKey(provinceCode))
      {
        ((List)map.get(provinceCode)).add(cityVO);
      }
      else
      {
        List<CityVO> subList = new ArrayList();
        subList.add(cityVO);
        map.put(provinceCode, subList);
      }
    }
    return map;
  }
  
  public Map<String, List<DistrictVO>> getDistrictMap()
  {
    List<DistrictVO> districtList = this.commonDAO.listAllDistrict();
    Map<String, List<DistrictVO>> map = new HashMap();
    for (DistrictVO districtVO : districtList)
    {
      String cityCode = districtVO.getCityCode();
      if (map.containsKey(cityCode))
      {
        ((List)map.get(cityCode)).add(districtVO);
      }
      else
      {
        List<DistrictVO> subList = new ArrayList();
        subList.add(districtVO);
        map.put(cityCode, subList);
      }
    }
    return map;
  }
  
  public void validateLevel(String correctLevel, String level)
  {
    if ((!StringUtils.isBlank(level)) && 
      (!level.equals(correctLevel)))
    {
      DzErrorCode.CommonErrorCode e = DzErrorCode.CommonErrorCode.LEVEL_ERROR;
      throw new BusinessException(e.value(), e.getDesc(), "区域代码districtCode和传入的参数level不匹配");
    }
  }
}
